<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) wangyt <wangtom365@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Http\Controllers\Admins;

use Illuminate\Http\Request;

use App\Http\Controllers\Admins\AdminController;
use Illuminate\Support\Facades\DB;

use App\Models\Post\PostModel;
use App\Models\Post\PostTextModel;
use App\Models\CategoryModel;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;


class PostsController extends AdminController
{
    /**
     * @var string
     */
    protected $moduleName = 'posts';

    /**
     * 列表
     * @return string
     */
    public function index(Request $request)
    {
        $page = $request->input('page', 0);
        $category = $request->input('category', '');
        $data = [];
        $where = [];
        if ($category) {
            $where['category'] = $category;
        }

        $result = (new PostModel())->getListWithPaginate($where, $page);
        if (empty($result)) {
            return '没有获取到数据.请确认URL是否正确.';
        }

        $data['categoryList'] = (new CategoryModel())->getList();
        // $data['categoryCount'] = (new PostModel())->getListGroupByCategory();
// print_r($data);exit;

        $data['listData'] = $result;

        return view('admins.posts.list', $data);
    }

    public function getPostInfo($postId = 0, $sectionId = 0)
    {
        $data = [
            'bookInfo' => [],
            'sectionList' => [],
            'sectionInfo' => [],
            'textInfo' => [],
        ];
        // 图书信息
        $data = (new PostModel())->findOrFail($postId)->toArray();
// var_dump($data);exit;
        $textId = !empty($data['text_id']) ? $data['text_id'] : 0;
        $textResult = (new PostTextModel())->findOrFail($textId)->toArray();

        $data['content'] = $textResult['content'];

        // // 章节信息
        // $where = [
        //     'book_id' => $bookId,
        // ];
        // $data['sectionList'] = (new PostTextModel())->findOrFail($where);

        // if ($sectionId) {
        //     // 当前章节
        //     $data['sectionInfo'] = collect($data['sectionList'])->where('id', $sectionId)->first();

        //     // 章节详情
        //     $textId = $data['sectionInfo']['text_id'];
        //     $data['textInfo'] = (new BookTextsModel())->find($textId)->toArray();
        // }

        return $data;
    }

    public function add(Request $request)
    {
        if ($request->input('title') !== null) {
            // 添加文本内容
            $editorValue = $request->get('editorValue', '');
            $textId = $this->createPostTexts($editorValue, 'add');

            $response = [
                'code' => 0,
                'msg' => '',
                'data' => [],
                'time' => time(),
            ];
            $input = [];
            $input['platform'] = 'posts';
            $input['category'] = $request->input('category', '');

            $input['title'] = $request->input('title');
            $input['source_name'] = $request->input('source_name', '网络');
            $input['source_link'] = $request->input('source_link');
            $input['text_id'] = $textId;
            $input['created_at'] = date('Y-m-d H:i:s');
            // $input['updated_at']  = date('Y-m-d H:i:s');

            $result = (new PostModel())->saveData(collect($input));

            if ($result) {
                $response['code'] = 1;
                $response['data'] = $result;
            }
        // print_r($input);
            return response($response, 200);
        }
        $data['categoryList'] = (new CategoryModel())->getListByModule($this->moduleName);

        return view('admins.posts.add', $data);
    }

    public function edit(Request $request, $id = 0)
    {
        if ($request->input('title') !== null) {
            // 添加文本内容
            $editorValue = $request->get('editorValue', '');
            $textId = $this->createPostTexts($editorValue, 'edit');

            $response = [
                'code' => 0,
                'msg' => '',
                'data' => [],
                'time' => time(),
            ];
            $input = [];
            $input['platform'] = 'posts';
            $input['category'] = $request->input('category', '');

            $input['title'] = $request->input('title');
            $input['source_name'] = $request->input('source_name', '网络');
            $input['source_link'] = $request->input('source_link');
            $input['text_id'] = $textId;
            // $input['created_at']  = date('Y-m-d H:i:s');
            $input['updated_at'] = date('Y-m-d H:i:s');

            $where = [
                'id' => $id,
            ];
            $result = (new PostModel())->updateData($where, $input);

            // $result = DB::table('ks_posts')
            //     ->where($where)
            //     ->update($input);
            if ($result) {
                $response['code'] = 1;
            }
        // print_r($input);
            return response($response, 200);
        }

        $result = $this->getPostInfo($id);
        // print_r($result);exit;

        $data['categoryList'] = (new CategoryModel())->getListByModule($this->moduleName);

        // $result = $result->toArray();
        $data['detail'] = $result;

        return view('admins.posts.edit', $data);
    }

    /**
     * @param string $content
     * @param string $remark
     * @return mixed
     */
    protected function createPostTexts(&$content = '', $remark = '')
    {
        // 添加文本内容
        // $editorValue = $request->get('editorValue', '');
        $input = [
            // 'book_id' => $bookId,
            'content' => $content,
            'remark' => $remark,
            'created_at' => date('Y-m-d H:i:s'),
        ];
        $textId = (new PostTextModel())->saveData(collect($input));

        return $textId;
    }
}
